package test;

import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;

import java.sql.SQLException;

public class Aop {

    @Test
    public void before() {
        WaiterImpl waiter = new WaiterImpl();

        GreetingBeforeAdvice greetingBeforeAdvice = new GreetingBeforeAdvice();

        ProxyFactory proxyFactory = new ProxyFactory();

        proxyFactory.setTarget(waiter);

        proxyFactory.addAdvice(greetingBeforeAdvice);

        Waiter proxy = (Waiter) proxyFactory.getProxy();

        proxy.greetTo("John");
        proxy.greetTo("Tom");
    }

    @Test
    public void after() {
        WaiterImpl waiter = new WaiterImpl();

        GreetingAfterAdvice greetingAfterAdvice = new GreetingAfterAdvice();

        ProxyFactory proxyFactory = new ProxyFactory();

        proxyFactory.setTarget(waiter);

        proxyFactory.addAdvice(greetingAfterAdvice);

        Waiter proxy = (Waiter) proxyFactory.getProxy();

        proxy.greetTo("John");
        proxy.greetTo("Tom");
    }

    @Test
    public void around() {
        WaiterImpl waiter = new WaiterImpl();

        GreetingAroundAdvice greetingAroundAdvice = new GreetingAroundAdvice();

        ProxyFactory proxyFactory = new ProxyFactory();

        proxyFactory.setTarget(waiter);

        proxyFactory.addAdvice(greetingAroundAdvice);

        Waiter proxy = (Waiter) proxyFactory.getProxy();

        proxy.greetTo("John");
        proxy.greetTo("Tom");
    }

    @Test
    public void e() throws SQLException {
        ForumService forumService = new ForumService();

        TransactionManager transactionManager = new TransactionManager();

        ProxyFactory proxyFactory = new ProxyFactory();

        proxyFactory.setTarget(forumService);

        proxyFactory.addAdvice(transactionManager);

        proxyFactory.setProxyTargetClass(true);

        ForumService proxy = (ForumService) proxyFactory.getProxy();

        //proxy.removeForum(123);
        proxy.updateForum();
    }


}
